import time
import tkinter as tk
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
from models.leave_model import LeaveModel
from dao.Result_his import Result
from middleWare import Middleware
from config.Instrument import Instrument_toml

class Main_leave(tk.Frame):
    def __init__(self, master):
        self.master = master
        self.isselc = False
        
    def windw(self):
        #创建一个文本域,文本内容为：新增请假
        self.text = Label(self.master, text="新增请假")
        #修改背景色
        self.text.config(bg="#fafafa")
        #修改字体
        self.text.config(font=("微软雅黑", 20))

        #创建一个文本域，文本内容为：请假类型
        self.text2 = Label(self.master, text="请假类型")
        #修改背景色
        self.text2.config(bg="#fafafa")
        #修改字体
        self.text2.config(font=("微软雅黑", 12))

        #设置一个输入框，默认输入：请输入请假类型
        self.txt_type = Entry(self.master, width=18,bg='#fafafa')
        self.txt_type.insert(0, "请输入请假类型")
        self.txt_type.config(font=("微软雅黑", 8))

        #设置一个文本域，文本为:请假天数
        self.text3 = Label(self.master, text="请假天数")
        #修改背景色
        self.text3.config(bg="#fafafa")
        #修改字体
        self.text3.config(font=("微软雅黑", 12))

        #设置一个输入框，默认输入：请输入请假天数
        self.txt_days = Entry(self.master, width=18,bg='#fafafa')
        self.txt_days.insert(0, "请输入请假天数")
        self.txt_days.config(font=("微软雅黑", 8))

        #设置一个文本域，文本为：请假事由
        self.text4 = Label(self.master, text="请假事由")
        #修改背景色
        self.text4.config(bg="#fafafa")
        #修改字体
        self.text4.config(font=("微软雅黑", 12))

        #设置一个Text域，文本为：描述一下请假事由
        # self.text5 = Label(self.master, text="描述一下请假事由")
        self.txt_reason = Text(self.master,bg='#ffffff',wrap=WORD)
        #修改字体
        self.txt_reason.config(font=("宋体", 8))
        #配置宽高
        self.txt_reason.config(width=40, height=8)

        #设置一个文本域，文本为：请假人
        self.text5 = Label(self.master, text="请  假  人")
        #修改背景色
        self.text5.config(bg="#fafafa")
        #修改字体
        self.text5.config(font=("微软雅黑", 12))

        #设置一个输入框，默认输入：请输入请假人
        self.txt_user = Entry(self.master, width=18,bg='#fafafa')
        self.txt_user.insert(0, "请输入请假人")
        self.txt_user.config(font=("微软雅黑", 8))

        #设置一个文本域，文本为：审核人
        self.text6 = Label(self.master, text="审  核  人")
        #修改背景色
        self.text6.config(bg="#fafafa")
        #修改字体
        self.text6.config(font=("微软雅黑", 12))

        #读取图片显示
        self.img = PhotoImage(file="static/用户头像.png")
        self.img_label = Label(self.master, image=self.img)
        #配置背景色
        self.img_label.config(bg="#fafafa")

        #设置一个区域显示文本。文本为：SiYuan
        self.text7 = Label(self.master, text="SiYuan")
        #修改背景色
        self.text7.config(bg="#fafafa")
        #修改字体
        self.text7.config(font=("微软雅黑", 10))

        #设置一个区域显示文本，文本内容为:标识
        self.text8 = Label(self.master, text="标        识")
        #修改背景色
        self.text8.config(bg="#fafafa")
        self.text8.config(font=("微软雅黑", 12))

        #读取图片显示
        self.img2 = PhotoImage(file="static/管理员.png")
        self.img_label2 = Label(self.master, image=self.img2)
        #配置背景色
        self.img_label2.config(bg="#fafafa")

        #设置一个区域显示文本。文本为：
        self.text9 = Label(self.master, text="科任教师")
        #修改背景色
        self.text9.config(bg="#fafafa")
        self.text9.config(font=("微软雅黑", 12))

        #设置一个按钮用于提交
        self.btn = Button(self.master, text="确认提交")#暂未实现
        #修改背景色
        self.btn.config(bg="#3b80f9",fg='#ffffff',command=self.submit)
        #配置宽高
        self.btn.config(width=30, height=2,relief="flat")


        #配置文本域，文本内容为：移出名单
        self.text10 = Label(self.master, text="移出考勤名单")
        #配置颜色
        self.text10.config(bg="#fafafa")
        self.text10.config(font=("微软雅黑", 8))

        #设置一个按钮，颜色为#3b80f9
        self.btn_slc = Button(self.master, text="否",command=lambda: self.select("否"))#选中与否按钮
        self.btn_slc.config(bg="#3b80f9",fg='#ffffff')
        #配置宽度
        self.btn_slc.config(width=5,height=1,relief='flat')

        #设置一个按钮，颜色为#929497
        self.btn_del = Button(self.master, text="是",command=lambda: self.select("是"))#选中与否按钮
        self.btn_del.config(bg="#929497",fg='#ffffff')
        #配置宽度
        self.btn_del.config(width=5,height=1,relief='flat')

    def Defult(self):
        self.txt_type.delete(0, END)
        self.txt_days.delete(0, END)
        self.txt_reason.delete('1.0', END)
        self.txt_user.delete(0, END)

    def submit(self):
        leav_type = self.txt_type.get()
        leave_day = self.txt_days.get()
        leave_desc = self.txt_reason.get('1.0',END)
        leave_user = self.txt_user.get()
        reviewedBy = self.text7.cget("text")
        leav = LeaveModel()
        leav.setleave('',leav_type,leave_day,leave_desc,leave_user,reviewedBy,time.strftime("%Y-%m-%d", time.localtime()))
        data = leav.getleave()
        dic = {}
        for i in data:
            if i != 'leave_id':
                dic[i] = data[i]
        
        mid = Middleware()
        ans = mid.data_binding_user_by_id(dic['reviewedBy'])
        dic['reviewedBy'] = ans['user_id']
        

        #TODO
        dic1 = self.get_config()
        res = Result(**dic1)
        res.start()
        res.insert(**dic)
        res.close()
        self.Defult()
        messagebox.showinfo("提示", "提交成功")

    def get_config(self):
        toml = Instrument_toml()
        user = toml.GetStrogeToml("user")
        password = toml.GetStrogeToml("password")
        database = toml.GetStrogeToml("database")
        url = toml.GetStrogeToml("url")

        return {
            "url": url,
            "user": user,
            "password": password,
            "db": database
        }



    def select(self,name):
        if name == "是":
            self.isselc = True
            self.btn_slc.config(bg="#929497",fg='#ffffff')
            self.btn_del.config(bg="#3b80f9",fg='#ffffff')
        else:
            self.isselc = False
            self.btn_slc.config(bg="#3b80f9",fg='#ffffff')
            self.btn_del.config(bg="#929497",fg='#ffffff')


    def hidden_windw(self):
        widgets_to_hide = [self.text, self.text2, self.txt_type, self.text3, self.txt_days, self.text4, self.txt_reason, self.text5, self.txt_user, self.text6, self.img_label, self.text7, self.text8, self.img_label2, self.text9, self.btn, self.text10, self.btn_slc, self.btn_del]
        for widget in widgets_to_hide:
            widget.place_forget()
    
    def un_hidden_windw(self):
        self.text.place(x=100, y=50)
        self.text2.place(x=120, y=100)
        #显示输入框
        self.txt_type.place(x=240, y=100)
        #显示文本域
        self.text3.place(x=120, y=150)
        #显示输入框
        self.txt_days.place(x=240, y=150)       
        #显示文本域
        self.text4.place(x=120, y=250)
        #显示文本域
        self.txt_reason.place(x=240, y=250)
        self.text5.place(x=120, y=200)
        #显示输入框
        self.txt_user.place(x=240, y=200)
        self.text6.place(x=120, y=350)
        self.img_label.place(x=240, y=355)
        self.text7.place(x=280, y=355)
        self.text8.place(x=120, y=400)
        self.img_label2.place(x=240, y=405)
        self.text9.place(x=280, y=403)
        self.btn.place(x=340, y=490)
        self.text10.place(x=120, y=505)
        self.btn_slc.place(x=220, y=500)
        self.btn_del.place(x=260, y=500)